package com.lys.book.dao;

import com.lys.book.model.BookInfo;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BookInfoDAO {
    private Connection connection;

    // 构造方法，用于初始化数据库连接
    public BookInfoDAO(Connection connection) {
        this.connection = connection;
    }

    // 插入数据
    public void insert(BookInfo book) throws SQLException {
        String sql = "INSERT INTO book_info (name, author, publisher, isbn, price) VALUES (?, ?, ?, ?, ?)";

        try (PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, book.getName());
            statement.setString(2, book.getAuthor());
            statement.setString(3, book.getPublisher());
            statement.setString(4, book.getIsbn());
            statement.setString(5, book.getPrice());

            statement.executeUpdate();
        }
    }

    // 删除数据
    public void delete(String isbn) throws SQLException {
        String sql = "DELETE FROM book_info WHERE isbn = ?";
        try (PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, isbn);
            statement.executeUpdate();
        }
    }

    // 更新数据
    public void update(BookInfo book) throws SQLException {
        String sql = "UPDATE book_info SET name = ?, author = ?, publisher = ?, price = ? WHERE isbn = ?";

        try (PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setString(1, book.getName());
            statement.setString(2, book.getAuthor());
            statement.setString(3, book.getPublisher());
            statement.setString(4, book.getPrice());
            statement.setString(5, book.getIsbn());
            statement.executeUpdate();
        }
    }

    // 查询所有数据
    public List<BookInfo> getAll() throws SQLException {
        List<BookInfo> books = new ArrayList<>();
        String sql = "SELECT * FROM book_info";

        try (Statement statement = connection.createStatement();
             ResultSet resultSet = statement.executeQuery(sql)) {
            while (resultSet.next()) {
                BookInfo book = new BookInfo();
                book.setName(resultSet.getString("name"));
                book.setAuthor(resultSet.getString("author"));
                book.setPublisher(resultSet.getString("publisher"));
                book.setIsbn(resultSet.getString("isbn"));
                book.setPrice(resultSet.getString("price"));

                books.add(book);
            }
        }

        return books;
    }

    // 根据ID查询数据
    public BookInfo getById(int id) throws SQLException {
        String sql = "SELECT * FROM book_info WHERE id = ?";

        try (PreparedStatement statement = connection.prepareStatement(sql)) {
            statement.setInt(1, id);

            try (ResultSet resultSet = statement.executeQuery()) {
                if (resultSet.next()) {
                    BookInfo book = new BookInfo();
                    book.setName(resultSet.getString("name"));
                    book.setAuthor(resultSet.getString("author"));
                    book.setPublisher(resultSet.getString("publisher"));
                    book.setIsbn(resultSet.getString("isbn"));
                    book.setPrice(resultSet.getString("price"));
                    return book;
                }
            }
        }

        return null;
    }
}
